package com.im.app.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.im.app.manager.ChatLogManager;
import com.im.app.mapper.ChatLogsMapper;
import com.im.app.model.ChatLogs;
import com.im.app.service.ChatLogsService;
import lombok.extern.slf4j.Slf4j;
import org.jim.core.cache.redis.JedisTemplate;
import org.jim.core.packets.ChatType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;


@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class ChatLogsServiceImpl implements ChatLogsService {

    @Resource
    private ChatLogsMapper chatLogsMapper;

    private void cacheGroupChatLogs(List<ChatLogs> chatLogs , String groupId){
        if(chatLogs == null || groupId == null) return;
        String key = "chatLogs_"+groupId;
        ChatLogManager.getManager().cacheChatLogsConversations(key,chatLogs);
        try {
            JedisTemplate.me().set(key,chatLogs,60*60*24);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private List<ChatLogs> getRedisGroupChatLogs(String key){
        List<ChatLogs> chatLogs = new ArrayList<>();
        try {
            List<JSONObject> list = JedisTemplate.me().get(key, List.class);
            for (JSONObject obj: list) {
                ChatLogs chatLogs1 = obj.toJavaObject(ChatLogs.class);
                chatLogs.add(chatLogs1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return chatLogs;
    }


    @Override
    public int insert(ChatLogs chatLogs) {
        int result = chatLogsMapper.insert(chatLogs);
        if(result>0){
            //群聊记录缓存
            if(chatLogs.getChatType() == ChatType.CHAT_TYPE_PUBLIC.getNumber()){
                List<ChatLogs> chatLogsList = chatLogsMapper.selectChatLogsList(new ChatLogs(chatLogs.getRecvId()));
                String groupId = chatLogs.getRecvId();
                cacheGroupChatLogs(chatLogsList,groupId);
            }
        }
        return result;
    }

    @Override
    public int cancelChatLog(String opUserId,String msgId) {
        return chatLogsMapper.withDram(msgId);
    }

    @Override
    public int clearChatLog(String opUserId, String userId) {
        return chatLogsMapper.clearChatLog(opUserId,userId);
    }

    @Override
    public int updateIsRead(String sendId, String recvId) {
        return chatLogsMapper.updateIsRead(sendId,recvId);
    }

    @Override
    public int updateIsReadById(ChatLogs chatLogs) {
        return chatLogsMapper.updateChatLogs(chatLogs);
    }

    @Override
    public int updateMsg(String userId,String nickName,String avatar) {
        return chatLogsMapper.updateMsg(userId,nickName,avatar);
    }


    @Override
    public List<ChatLogs> getChatLogsList(String opUserId, String userId, Long sendTime) {
        return chatLogsMapper.getChatLogsList(opUserId,userId,sendTime);
    }

    @Override
    public ChatLogs getChatLogsById(String msgId) {
        return chatLogsMapper.selectById(msgId);
    }

    @Override
    public List<ChatLogs> getGroupHistoryMessage(String groupId, Long sendTime) {
        return chatLogsMapper.getGroupHistoryMessage(groupId,sendTime);
    }

    @Override
    public List<ChatLogs> selectChatLogsList(ChatLogs chatLogs) {
        return chatLogsMapper.selectChatLogsList(chatLogs);
    }

    @Override
    public int getTodayChatLogs(String userId) {
        return chatLogsMapper.getTodayChatLogs(userId);
    }



}

